package de.fhkl.android.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.xmlpull.v1.XmlPullParser;

import android.content.Context;
import android.content.res.XmlResourceParser;
import android.util.Log;
import de.fhkl.android.R;
import org.xmlpull.v1.XmlSerializer;  

public class XmlUtil {
	private Context context;
	private XmlPullParser xxp;
	private String tag = "XMLReader";
	private DocumentBuilderFactory domfac;
	private DocumentBuilder dombuilder;
	private String url = "http://localhost:8080/XmlServerTest/searchpeople.jsp";

	public String eregi_replace(String strFrom, String strTo, String strTarget) {
		String strPattern = "(?i)" + strFrom;
		Pattern p = Pattern.compile(strPattern);
		Matcher m = p.matcher(strTarget);
		if (m.find()) {
			return strTarget.replaceAll(strFrom, strTo);
		} else {
			return strTarget;
		}
	}

	public XmlUtil(Context context) {
		String uriAPI = "http://192.168.1.103:8080/XmlServerTest/searchpeople.jsp";
		HttpGet httpRequest = new HttpGet(uriAPI);
		try {
			
			HttpResponse httpResponse = new DefaultHttpClient()
					.execute(httpRequest);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {

				String strResult = EntityUtils.toString(httpResponse
						.getEntity());

				strResult = eregi_replace("(\r\n|\r|\n|\n\r)", "", strResult);
			} else {
				Log.e(tag, "Error Response: "
						+ httpResponse.getStatusLine().toString());
			}
		} catch (Exception e) {
			Log.e(tag, e.getMessage().toString());
			e.printStackTrace();
		}
	}

	public XmlUtil(Context context, int s) {
		this.context = context;
		this.xxp = context.getResources().getXml(R.xml.person_sample);
		Log.i(tag, xxp.toString());
		domfac = DocumentBuilderFactory.newInstance();

		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		List<NameValuePair> _params = null;
		String result = null;
		// if(_params != null){
		// try {
		// httpPost.setEntity(new UrlEncodedFormEntity(_params));
		// } catch (UnsupportedEncodingException e1) {
		// e1.printStackTrace();
		// }
		// }
		HttpResponse httpResponse = null;
		try {
			Log.i(tag, ("httpResponse==null  " + (httpResponse == null)));
			httpResponse = httpClient.execute(httpPost);
			Log.i(tag, ("httpResponse==null  " + (httpResponse == null)));
		} catch (ClientProtocolException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		HttpEntity httpEntity = httpResponse.getEntity();
		try {
			result = EntityUtils.toString(httpEntity);
		} catch (ParseException e1) {

			e1.printStackTrace();
		} catch (IOException e1) {

			e1.printStackTrace();
		}

		try {
			dombuilder = domfac.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		try {
			InputStream is = new FileInputStream(url);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		Log.w(tag, result);
	}
	

	public String getValue(String tagName) {
		String value = "default";
		Log.i(tag, "1");
		try {
			while (xxp.getEventType() != XmlPullParser.END_DOCUMENT) {
				// System.out.println(xxp.getName()+"!");
				if (xxp.getEventType() == XmlPullParser.START_TAG) {
					if (xxp.getName().equals(tagName)) {
						value = xxp.nextText();
					}
					Log.i(tag, (xxp.getName() == null) + "");
					if (xxp.getName() != null) {
						Log.w(tag, xxp.getName());
					}
				}
				xxp.next();
			}
		} catch (Exception e) {

			return value;
		}
		return value;
	}

	public String convert(Context context, int templateId,
			ArrayList<String> values) {
		XmlResourceParser parse = context.getResources().getXml(templateId);
		try {
			while (parse.getEventType() != XmlResourceParser.END_DOCUMENT) {
				if (parse.getEventType() == XmlResourceParser.START_TAG) {

				} else if (parse.getEventType() == XmlPullParser.END_TAG) {
				} else if (parse.getEventType() == XmlPullParser.TEXT) {
				}
				parse.next();
			}
		} catch (Exception e) {
			Log.e(tag, e.getMessage());
		}

		return null;
	}
}
